package sample.stock;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class EODData extends Activity{
	private Spinner exchange = null;
	private AutoCompleteTextView symbol = null;
	private Button check = null;

	private String ex;
	
	@SuppressWarnings("unchecked")
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.eod);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
        exchange = (Spinner)findViewById(R.id.spinnerExchange);
        symbol = (AutoCompleteTextView)findViewById(R.id.autoCompleteSymbol);
        check = (Button)findViewById(R.id.check);
        
        final ArrayAdapter adapter = ArrayAdapter.createFromResource(
                this, R.array.exchange, android.R.layout.simple_spinner_item);
        
        final ArrayAdapter adapterAMEX = ArrayAdapter.createFromResource(this, R.array.AMEX, R.layout.list_ticker);
        final ArrayAdapter adapterASX = ArrayAdapter.createFromResource(this, R.array.ASX, R.layout.list_ticker);
        final ArrayAdapter adapterCBOT = ArrayAdapter.createFromResource(this, R.array.CBOT, R.layout.list_ticker);
        final ArrayAdapter adapterCFE = ArrayAdapter.createFromResource(this, R.array.CFE, R.layout.list_ticker);
        final ArrayAdapter adapterCME = ArrayAdapter.createFromResource(this, R.array.CME, R.layout.list_ticker);
        final ArrayAdapter adapterCOMEX = ArrayAdapter.createFromResource(this, R.array.COMEX, R.layout.list_ticker);
        final ArrayAdapter adapterFOREX = ArrayAdapter.createFromResource(this, R.array.FOREX, R.layout.list_ticker);
        final ArrayAdapter adapterHKEX = ArrayAdapter.createFromResource(this, R.array.HKEX, R.layout.list_ticker);
        final ArrayAdapter adapterINDEX = ArrayAdapter.createFromResource(this, R.array.INDEX, R.layout.list_ticker);
        final ArrayAdapter adapterKCBT = ArrayAdapter.createFromResource(this, R.array.KCBT, R.layout.list_ticker);
        final ArrayAdapter adapterLIFFE = ArrayAdapter.createFromResource(this, R.array.LIFFE, R.layout.list_ticker);
        final ArrayAdapter adapterLSE = ArrayAdapter.createFromResource(this, R.array.LSE, R.layout.list_ticker);
        final ArrayAdapter adapterMGEX = ArrayAdapter.createFromResource(this, R.array.MGEX, R.layout.list_ticker);
        final ArrayAdapter adapterMLSE = ArrayAdapter.createFromResource(this, R.array.MLSE, R.layout.list_ticker);
        final ArrayAdapter adapterNASDAQ = ArrayAdapter.createFromResource(this, R.array.NASDAQ, R.layout.list_ticker);
        final ArrayAdapter adapterNYBOT = ArrayAdapter.createFromResource(this, R.array.NYBOT, R.layout.list_ticker);
        final ArrayAdapter adapterNYMEX = ArrayAdapter.createFromResource(this, R.array.NYMEX, R.layout.list_ticker);
        final ArrayAdapter adapterNYSE = ArrayAdapter.createFromResource(this, R.array.NYSE, R.layout.list_ticker);
        final ArrayAdapter adapterOPRA = ArrayAdapter.createFromResource(this, R.array.OPRA, R.layout.list_ticker);
        final ArrayAdapter adapterOTCBB = ArrayAdapter.createFromResource(this, R.array.ASX, R.layout.list_ticker);
        
        exchange.setAdapter(adapter);
        exchange.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				switch(exchange.getSelectedItemPosition()){
				case 0: default:
					ex = "AMEX";
					symbol.setAdapter(adapterAMEX);
					break;
				case 1:
					ex = "ASX";
					symbol.setAdapter(adapterASX);
					break;
				case 2:
					ex = "CBOT";
					symbol.setAdapter(adapterCBOT);
					break;
				case 3:
					ex = "CFE";
					symbol.setAdapter(adapterCFE);
					break;
				case 4:
					ex = "CME";
					symbol.setAdapter(adapterCME);
					break;
				case 5:
					ex = "COMEX";
					symbol.setAdapter(adapterCOMEX);
					break;
				case 6:
					ex = "FOREX";
					symbol.setAdapter(adapterFOREX);
					break;
				case 7:
					ex = "HKEX";
					symbol.setAdapter(adapterHKEX);
					break;
				case 8:
					ex = "INDEX";
					symbol.setAdapter(adapterINDEX);
					break;
				case 9:
					ex = "KBCT";
					symbol.setAdapter(adapterKCBT);
					break;
				case 10:
					ex = "LIFFE";
					symbol.setAdapter(adapterLIFFE);
					break;
				case 11:
					ex = "LSE";
					symbol.setAdapter(adapterLSE);
					break;
				case 12:
					ex = "MGEX";
					symbol.setAdapter(adapterMGEX);
					break;
				case 13:
					ex = "MLSE";
					symbol.setAdapter(adapterMLSE);
					break;
				case 14:
					ex = "NASDAQ";
					symbol.setAdapter(adapterNASDAQ);
					break;
				case 15:
					ex = "NYBOT";
					symbol.setAdapter(adapterNYBOT);
					break;
				case 16:
					ex = "NYMEX";
					symbol.setAdapter(adapterNYMEX);
					break;
				case 17:
					ex = "NYSE";
					symbol.setAdapter(adapterNYSE);
					break;
				case 18:
					ex = "OPRA";
					symbol.setAdapter(adapterOPRA);
					break;
				case 19:
					ex = "OTBB";
					symbol.setAdapter(adapterOTCBB);
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				Toast.makeText(getApplicationContext(), "Nothing selected", Toast.LENGTH_SHORT).show();
			}
		});
        
        check.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent chartView = new Intent(EODData.this, EODChart.class);
				chartView.putExtra("symbol", symbol.getText().toString());
				chartView.putExtra("exchange",ex);
				startActivity(chartView);
			}
		});
    }
}
